package com.wanhe.base.net;


import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.wanhe.base.model.entity.DishesTypeDetailEntity;
import com.wanhe.base.model.entity.CallRing;
import com.wanhe.base.model.entity.LoginEntity;
import com.wanhe.base.model.entity.MenuDesignEntity;
import com.wanhe.base.model.entity.SellOutResultEntity;
import com.wanhe.base.model.entity.TableBillEntity;
import com.wanhe.base.model.entity.TableResultEntity;
import com.wanhe.base.model.entity.VersionEntity;
import com.wanhe.base.util.SPUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;


/**
 * Created by Administrator on 2017/3/14.
 */
@Singleton
public class ServerApi {

    ServerApiImp serverApiImp;
    RequestTool requestTool;

    @Inject
    public ServerApi() {

    }

    public String mapToJson(Map<String, Object> map) {
        Gson gson = new Gson();
        return gson.toJson(map);
    }


    /**
     *检查登录
     * @param ip
     * @param port
     * @param OperID
     * @param Password
     * @return
     */
    public Observable<LoginEntity> checkLogin(String ip, String port, String OperID, String Password) {
        String url = "http://" + ip + ":" + port;
        Log.d("checkLogin", "url=" + url);
        if (requestTool != null && !TextUtils.isEmpty(SPUtils.getUrl())) {
            requestTool.resetRetrofit(url);
        } else {
            requestTool = new RequestTool(url);
        }
        serverApiImp = requestTool.getServerApi();
        Map<String, Object> body = new HashMap();
        Map<String, Object> param = new HashMap();
        param.put("op", "LoginPadMenu");
        param.put("OperID", OperID);
        param.put("Password", Password);
        String Param = mapToJson(param);
        body.put("paramJsonStr", Param);
        Log.d("checkLogin", "paramJsonStr=" + Param);
        return serverApiImp.login(body);
    }

    /**
     * 登录
     * @param OperID
     * @param Password
     * @return
     */
    public Observable<LoginEntity> login(String OperID, String Password) {
        if (requestTool == null) {
            requestTool = new RequestTool(SPUtils.getUrl());
        }
        serverApiImp = requestTool.getServerApi();
        Map<String, Object> body = new HashMap();
        Map<String, Object> param = new HashMap();
        param.put("op", "LoginPadMenu");
        param.put("OperID", OperID);
        param.put("Password", Password);
        String Param = mapToJson(param);
        body.put("paramJsonStr", Param);
        return serverApiImp.login(body);
    }

    /**
     * 获取所有菜品
     * @return
     */
    public Observable<DishesTypeDetailEntity> getAllDish() {
        if (requestTool == null) {
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        Map<String, Object> body = new HashMap();
        param.put("op", "GetAll");
        String Param = mapToJson(param);
        body.put("paramJsonStr", Param);
        return serverApiImp.getAll(body);
    }

    /**
     * 获取估清数据
     * @return
     */

    public Observable<SellOutResultEntity> getSellOutData() {
        if(requestTool==null){
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        param.put("op", "PollDishWarnList");
        String strParam = mapToJson(param);
        Map<String, Object> body = new HashMap();
        body.put("paramJsonStr", strParam);
        return serverApiImp.batchOrder(body);
    }

    /**
     * 获取设计器数据
     * @return
     */
    public Observable<MenuDesignEntity> getMenuDesignData() {
        if (requestTool == null) {
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        Map<String, Object> body = new HashMap();
        param.put("op", "GetMenuDesignData");
        String Param = mapToJson(param);
        body.put("paramJsonStr", Param);
        return serverApiImp.getMenuDesign(body);
    }

    /**
     * 获取table list
     * @param PageID
     * @param TableNum
     * @return
     */
    public Observable<TableResultEntity> GetTables(String PageID, String TableNum) {
        if (requestTool == null) {
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        Map<String, Object> body = new HashMap();
        param.put("op", "GetTables");
        param.put("PageID", PageID);
        param.put("TableNum", TableNum);
        String Param = mapToJson(param);
        body.put("paramJsonStr", Param);
        return serverApiImp.getTables(body);
    }

    /**
     * 批量下单
     * @param batchOrder
     * @return
     */
    public Observable<SellOutResultEntity> batchOrder(String batchOrder) {
        if(requestTool==null){
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> body = new HashMap();
        body.put("paramJsonStr", batchOrder);
        return serverApiImp.batchOrder(body);
    }



    public Observable<CallRing> callRing(String tableName, String message) {
        if(requestTool==null){
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        param.put("op", "Ring");
        param.put("tableName", tableName);
        param.put("message", message);
        String strParam = mapToJson(param);
        Map<String, Object> body = new HashMap();
        body.put("paramJsonStr", strParam);
        return serverApiImp.callRing(body);
    }

    /**
     * 获取账单列表
     * @param posTableId
     * @param operId
     * @param password
     * @return
     */
    public Observable<TableBillEntity> getTableBill(String posTableId, String operId, String password) {
        if(requestTool==null){
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        param.put("posTableId", posTableId);
        param.put("operId", operId);
        param.put("password", password);
        String strParam = mapToJson(param);
        Map<String, Object> body = new HashMap();
        body.put("paramJsonStr", strParam);
        return serverApiImp.getTableBill(body);
    }


    /**
     * 获取版本
     * @return
     */
    public Observable<VersionEntity> getVersion() {
        if(requestTool==null){
            requestTool = new RequestTool(SPUtils.getUrl());
            serverApiImp = requestTool.getServerApi();
        }
        Map<String, Object> param = new HashMap();
        param.put("op", "GetApkVersion");
        String strParam = mapToJson(param);
        Map<String, Object> body = new HashMap();
        body.put("paramJsonStr", strParam);
        return serverApiImp.getVersion(body);
    }
}
